package com.litq.education.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.litq.education.bean.CustomerTable;
import com.litq.education.mapper.CustomerMapper;
import com.litq.education.service.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;
import org.thymeleaf.expression.Maps;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CustomerServiceImpl 实现类
 *
 * @author litq
 * @version v1.0
 * @since 2019/2/10 1:31
 */
@Service("CustomerService")
public class CustomerServiceImpl implements CustomerService {
    private static final Logger logger = LoggerFactory.getLogger(CustomerServiceImpl.class);
    @Resource

    private CustomerMapper CustomerMapper;

    /**
     * 查询客户所有信息
     *
     * @param current 一页多少行 number 第几页
     * @return 返回list
     * @author Litq
     * @version 1.0
     */
    @Override
    public Page<Map<String, Object>> selectListPage(int current, int number) {
        logger.info("----------------------------------开始进行查询逻辑处理");
        // 新建分页
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(current, number);

        // 返回分页结果 1为id
        logger.info("----------------------------------查询结果打印" + page.setRecords(this.CustomerMapper.dyGetUserList(page, 1)));
        return page.setRecords(this.CustomerMapper.dyGetUserList(page, 1));
    }

    /**
     * 查询客户信息
     *
     * @param username 姓名
     * @return 返回list
     * @author Litq
     * @version 1.0
     */
    @Override
    public List selectListPageByUserName(String username) {
        logger.info("----------------------------------开始进行查询逻辑处理");

        logger.info("----------------------------------查询结果打印" + (List) CustomerMapper.dyGetUserListByUsername(username));
        return (List) CustomerMapper.dyGetUserListByUsername(username);
    }


    /**
     * 查询信息总数
     *
     * @return 返回总数
     * @author Litq
     * @version 1.0
     */
    @Override
    public int selectListcount() {

        logger.info("----------------------------------开始进行查询逻辑处理");
        return CustomerMapper.count();

    }

    /**
     * 根据id删除客户
     *
     * @param id 客户id
     * @return 0-->成功
     * @author Litq
     * @version 1.0
     */
    @Override
    public int deleteById(int id) {

        logger.info("----------------------------------开始进行删除逻辑处理");
        logger.info("----------------------------------打印要删除的id号码:" + id);

        CustomerTable ct = CustomerMapper.selectById(id);
        int count = CustomerMapper.countByGroupId(ct.getGroupId());
        int count1 = count-1;
        int sign2 = CustomerMapper.updateCount(count1);
        int sign = CustomerMapper.deleteById(id);

        logger.info("----------------------------------返回结果:" + sign);
        return sign;
    }

    /**
     * 根据id查询客户
     *
     * @param id 客户id
     * @return list
     * @author Litq
     * @version 1.0
     */
    @Override
    public Map findCustomerById(int id) {
        logger.info("----------------------------------开始进行查询逻辑处理");
        CustomerTable ct = CustomerMapper.selectById(id);

        Map<String, Object> map = new HashMap();
        if (ct != null) {
            BeanMap beanMap = BeanMap.create(ct);
            for (Object key : beanMap.keySet()) {
                map.put(key + "", beanMap.get(key));
            }
        }
        logger.info("----------------------------------打印查询结果" + map);
        return map;
    }

    /**
     * 修改客户信息
     *
     * @param reqMap 客户信息
     * @return list
     * @author Litq
     * @version 1.0
     */
    @Override
    public int updateCustomerById(Map reqMap) {
        logger.info("----------------------------------开始进行修改逻辑处理");
        CustomerTable ct = new CustomerTable();

        String groupId = reqMap.get("groupId").toString();
        int sign = 0;
        //设定 如果已经存在发起者，则不能新增发起者
        Map isCapMap = CustomerMapper.selectIsCap(groupId);
        logger.info("----------------------------------发起者"+isCapMap);
        //如果存在发起者
        if(!isCapMap.isEmpty()){
            //判断当前修改者 是不是 发起者
            //已经存在发起者的姓名
            String name = isCapMap.get("user_name").toString();
            //获取传入的姓名
            String sendName = reqMap.get("name").toString();
            //不一致 则为存在发起者
            if(!name.equals(sendName))
            {
                sign = 9999;
                return sign;
            }
            else{
                //则为发起者修改
                ct.setId(Integer.parseInt(reqMap.get("id").toString()));
                ct.setGrade(reqMap.get("grade").toString());
                ct.setGroupId(reqMap.get("groupId").toString());
                ct.setParentName(reqMap.get("parentName").toString());
                ct.setSchool(reqMap.get("school").toString());
                ct.setUserName(reqMap.get("name").toString());
                ct.setSex(reqMap.get("sex").toString());
                sign = CustomerMapper.updateById(ct);
                logger.info("----------------------------------打印处理结果" + sign);
                return sign;

            }
        }
        //不存在发起者 直接进行修改
        ct.setId(Integer.parseInt(reqMap.get("id").toString()));
        ct.setGrade(reqMap.get("grade").toString());
        ct.setGroupId(reqMap.get("groupId").toString());
        ct.setParentName(reqMap.get("parentName").toString());
        ct.setSchool(reqMap.get("school").toString());
        ct.setUserName(reqMap.get("name").toString());
        ct.setSex(reqMap.get("sex").toString());
        sign = CustomerMapper.updateById(ct);
        logger.info("----------------------------------打印处理结果" + sign);
        return sign;
    }

    /**
     * 插入一条客户信息
     *
     * @param reqMap 客户信息
     * @return list
     * @author Litq
     * @version 1.0
     * @return 999 人数已经满员  000 人数相等  1插入成功
     */
    @Override
    public int insertCustomer(Map reqMap) {
        logger.info("----------------------------------开始进行插入逻辑处理");
        logger.info("----------------------------------是否存在参数"+reqMap);
        //此groupId总人数不能大于设定人数
        String groupId = reqMap.get("groupId").toString();
        int count = CustomerMapper.countByGroupId(groupId);
        int zongCount = CustomerMapper.zongCountByGroupId(groupId);
        logger.info("----------------------------------系统设定人数"+zongCount);
        logger.info("----------------------------------已经存在人数"+count);
        int sign = 999;
        //设定 如果已经存在发起者，则不能新增发起者
        Map isCapMap = CustomerMapper.selectIsCap(groupId);
        logger.info("----------------------------------发起者"+isCapMap);
        //如果存在发起者 则判断添加的用户是不是发起者
        if(!isCapMap.isEmpty()){

            String cap = reqMap.get("isCap").toString();
            //是发起者
            if(cap.equals("1"))
            {
                sign = 9999;
                return sign;
            }
        }

        if(zongCount > count)
        {
            CustomerTable ct = new CustomerTable();
            //ct.setId(Integer.parseInt(reqMap.get("id").toString()));
            ct.setGrade(reqMap.get("grade").toString());
            ct.setGroupId(reqMap.get("groupId").toString());
            ct.setParentName(reqMap.get("parentName").toString());
            ct.setParentPhone(reqMap.get("parentPhone").toString());
            ct.setSchool(reqMap.get("school").toString());
            ct.setUserName(reqMap.get("name").toString());
            ct.setIsCap(reqMap.get("isCap").toString());
            ct.setSex(reqMap.get("sex").toString());
            sign= CustomerMapper.insert(ct);
            //每添加一条数据拼团人数加一
            int count1 = count+1;
            int sign2 = CustomerMapper.updateCount(count1);
            logger.info("----------------------------------每添加一条数据拼团人数加"+sign2);

        }
        else if(zongCount == count)
        {
            sign = 000;
        }
        logger.info("----------------------------------返回结果"+sign);
        return sign;
    }

}
